#pragma once

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>

namespace bit
{
	//仿函数 可以像操作函数一样操作对象 因为重载了() 不是什么新语法
	template<class T>
	class less
	{
	public:
		bool operator()(const T& a, const T& b)
		{
			return a < b;
		}
	};

	template<class T>
	class greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T, class Container = std::vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:

		priority_queue() = default;    // 强制生成默认构造函数

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_con.push_back(*first);
			}

			for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
			{
				adjust_down(i);
			}
		}

		void adjust_down(size_t parent)
		{
			Compare comfunc;

			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && comfunc(_con[child], _con[child + 1]))
				{
					++child;
				}

				if (comfunc(_con[parent], _con[child]))
				{
					std::swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else break;
			}
		}

		void adjust_up(size_t child)
		{
			Compare comfunc;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (comfunc(_con[parent], _con[child])) std::swap(_con[parent], _con[child]);
				else break;
				child = parent;
				parent = (child - 1) / 2;
			}
		}
		


		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size() - 1);
		}

		T& top()
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}

		void pop()
		{
			std::swap(_con[0], _con[_con.size() - 1]);

			_con.pop_back();

			adjust_down(0);
		}

	private:

		Container _con;

		Compare comp;
	};
}